package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author ：靳鸿鸿鸿曦
 * @date ：Created in 2022年06月09日
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;

    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam Integer year, @RequestParam Integer month) {
        List<OrderSetting> orderSettings = orderSettingService.getOrderSettingByMonth(year, month);
        List<Map<String, Object>> voList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(orderSetting.getOrderDate());
            Map<String, Object> map = new HashMap<>();
            map.put("date", cal.get(Calendar.DAY_OF_MONTH));
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            voList.add(map);
        }
        return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS, voList);
    }

    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
            log.info("[预约设置-根据日期编辑]data:{}",orderSetting);
        orderSettingService.editNumberByDate(orderSetting.getOrderDate(),orderSetting.getNumber());
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
    }


    @PostMapping("upload")
    public Result add(@RequestParam("excelFile") MultipartFile multipartFile) {
        try {
            InputStream inputStream = multipartFile.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheetAt = workbook.getSheetAt(0);
            //获取当前工作表最后一行的行号
            int lastRowNum = sheetAt.getLastRowNum();
            List<OrderSetting> list = new ArrayList<>();

            for (int i = 1; i <= lastRowNum; i++) {
                OrderSetting orderSetting = new OrderSetting();
                //根据行号获取行对象
                XSSFRow row = sheetAt.getRow(i);
                //获取当前行的第一个单元格位置
                short lastCellNum = row.getLastCellNum();

                for (int j = 0; j < lastCellNum; j++) {
                    if (j == 0) {
                        XSSFCell cell = row.getCell(j);
                        Date dateCellValue = cell.getDateCellValue();
                        orderSetting.setOrderDate(dateCellValue);
                    }
                    if (j == 1) {
                        double numericCellValue = row.getCell(j).getNumericCellValue();

                        int i1 = Double.valueOf(numericCellValue).intValue();
                        orderSetting.setNumber(i1);

                    }

                }
                list.add(orderSetting);

            }
            orderSettingService.add(list);
            workbook.close();
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }


    }

}
